package com.zhuhjay;

import com.alibaba.druid.pool.DruidDataSource;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.zhuhjay.template.CustomTemplate;

import javax.sql.DataSource;

/**
 * @author ZhuHJay
 * @date 2023/4/24 20:10
 */
public class GeneratorCode {

    /** 需定制的数据信息 */
    private static final String HOST = "localhost";
    private static final String PORT = "3306";
    private static final String DATABASE = "book_system";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    private static final String AUTHOR = "ZhuHJay";
    private static final String PACKAGE_PREFIX = "com.zhuhjay";

    /** 数据库连接信息 */
    private static final String MYSQL_URL_PREFIX = "jdbc:mysql://";
    private static final String MYSQL_URL_SUFFIX =
            "?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&serverTimezone=UTC";

    public static void main(String[] args) {
        new Generator(getDataSource(), getGlobalConfig()).generate();
    }

    private static GlobalConfig getGlobalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setEntityPackage(PACKAGE_PREFIX + ".model.entity");
        globalConfig.setMapperPackage(PACKAGE_PREFIX + ".mapper");
        globalConfig.setMapperGenerateEnable(true);
        globalConfig.setMapperOverwriteEnable(true);
        globalConfig.setTemplateEngine(new CustomTemplate(AUTHOR));
        return globalConfig;
    }

    private static DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(getMysqlUrl());
        dataSource.setUsername(USERNAME);
        dataSource.setPassword(PASSWORD);
        return dataSource;
    }

    private static String getMysqlUrl() {
        return MYSQL_URL_PREFIX + HOST + ':' + PORT + '/' + DATABASE + MYSQL_URL_SUFFIX;
    }

}
